# -*- coding: utf-8 -*-
# @File     : services.py
# @Author   : bingjia
# @Time     : 2020/7/27 15:36
# @Desc     : 提供user模块的所有额外函数
import uuid

from django.utils.translation import ugettext as _

from ..base.api import exceptions as exc
from ..base.utils.urls import get_photo_url
from ..base.utils.files import get_file_path


def get_default_uuid():
    """
    产生uuid
    :return:
    """
    return uuid.uuid4().hex


def get_user_photo_url(user):
    """
    获取用户头像url
    :param user:
    :return:
    """
    if not user:
        return None
    return get_photo_url(user.photo)


def get_user_file_path(instance, filename):
    """
    获取用户文件路径
    :param instance:
    :param filename:
    :return:
    """
    return get_file_path(instance, filename, "user")


def get_message_attachement_path():
    """
    获取消息附件路径
    :return:
    """


def is_same_of_token_and_pk(user, model, pk):
    """
    判断token对应的用户和id是否一致
    """
    if user != model.get(id=pk):
        raise exc.AuthenticationFailed(_("Token and PK are not consistent"))